#include "ParsePNG.h"

int parse_png(char* filepath,struct Meta* ret_m)
{
	FILE* fp;
	int i;

	long OFFSET = 0;

	unsigned char Png[4];
	unsigned char IHDR_header[4];
	unsigned char HEIGHT[4];
	unsigned char WIDTH[4];

	int W=0;
	int H=0;




	fp = fopen(filepath,"rb");

	ret_m->fileSize = GetFileLength(fp);

	ret_m->upnpClass=(char*)malloc(strlen(UPNP_PICTURE)+1);
	strcpy(ret_m->upnpClass,UPNP_PICTURE);



	fread(Png,sizeof(unsigned char),(size_t)4,fp);
	OFFSET+=12;

	fseek(fp,OFFSET,SEEK_SET);
	fread(IHDR_header,sizeof(unsigned char),(size_t)4,fp);
	fread(HEIGHT,sizeof(unsigned char),(size_t)4, fp);
	fread(WIDTH, sizeof(unsigned char),(size_t)4,fp);


	W = getIntFrom4Byte(WIDTH);
	H = getIntFrom4Byte(HEIGHT);

	ret_m->ResolutionX=W;
	ret_m->ResolutionY=H;
	


	if(W <=640 && H <=480)
	{
		ret_m->ProtocolInfo = (char*)malloc(strlen(PROTOCOL_PNG_SM)+1);
		strcpy(ret_m->ProtocolInfo,PROTOCOL_PNG_SM);
	}else if(W <=1024 && H <= 768 )
	{
		ret_m->ProtocolInfo = (char*)malloc(strlen(PROTOCOL_PNG_MED)+1);
		strcpy(ret_m->ProtocolInfo,PROTOCOL_PNG_MED);
	}
	else
	{
		ret_m->ProtocolInfo = (char*)malloc(strlen(PROTOCOL_PNG_LRG)+1);
		strcpy(ret_m->ProtocolInfo,PROTOCOL_PNG_LRG);
	}


/*
	printf("\n-JPG Parsing Results.------\n");
	printf("File Size : %d\n",ret_m->fileSize);
	printf("Resolution: %dx%d\n",ret_m->ResolutionX,ret_m->ResolutionY);
	printf("Protocol  : %s\n",ret_m->ProtocolInfo);
	printf("UpnpClass : %s\n",ret_m->upnpClass);
	printf("-------------------------");
*/

	fclose(fp);
	return FILE_PICTURE;
}